package com.gw.booking.controller.merchant;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.wechat.*;
import com.gw.booking.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;


@Api(tags = {"商家-订单管理"})
@Controller
@RequestMapping("/merchant/orders")
public class MOrderController {


    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "订单列表（商家）")
    @RequestMapping(value = "/merchantOrderList.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<WMerchantOrderListDtoOut>> merchantOrderList(HttpServletRequest request
            , @ApiParam(value = "订单时间：yyyy-MM-dd") @RequestParam(required = false) String orderDate
            , @ApiParam(value = "订单状态:全部,0预约确认中,1预约完成,2到店,3取消,4却下,5已超时",
            allowableValues = "0,1,2,3,4,5") @RequestParam(required = false) Integer state
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.listForMerchant(Util.getLoginToken(request), orderDate, state, page, size));
    }

    @ApiOperation(value = "订单详情（商家）")
    @RequestMapping(value = "/merchantOrderDetail.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<WOrderDetailDtoOut> merchantOrderDetail(HttpServletRequest request
            , @ApiParam(value = "订单id", required = true) @RequestParam Integer orderId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.detailForMerchant(Util.getLoginToken(request), orderId));
    }

    @ApiOperation(value = "操作订单（商家）")
    @RequestMapping(value = "/merchantOrder.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject merchantOrder(HttpServletRequest request
            , @Valid @RequestBody WMerchantOrderDto wMerchantOrderDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        orderService.txUpdateByMerchant(Util.getLoginToken(request), wMerchantOrderDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "编辑订单（商家）(作废)")
    @RequestMapping(value = "/merchantChangeOrder.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject merchantChangeOrder(HttpServletRequest request
            , @Valid @RequestBody WMerchantChangeOrderDto wMerchantChangeOrderDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        orderService.txChangeByMerchant(Util.getLoginToken(request), wMerchantChangeOrderDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
